import { BizKfExtraProColumnType } from '@/components';
import * as API from '@/services';
import { Divider, message, Space, Typography } from 'antd';
import {getBooleanToProTableValueEnum, KfModal, KfPagingTablePage, KfPagingTablePagePropsAction, useBackendEnum} from 'kf-web-library';
import React, { useRef } from 'react';
import { useAccess, useLocation } from 'umi';
import QuickRoleForm from './components/QuickForm';
import './index.less';
import {disableJournalSpecialColumn, enableJournalSpecialColumn} from "@/services";

type Entity = API.JournalSpecialColumn;
type EntityPageQueryParams = API.JournalSpecialColumnPageQueryParams;
const entityTypeName = '特色栏目聚焦';
const EntityQuickForm = QuickRoleForm;

interface EntityTableListProps {
}

/**
 * 特色栏目聚焦 列表页面
 */
const EntityTableList: React.FC<EntityTableListProps> = (props) => {
  const location = useLocation();
  const access = useAccess();
  const {enumDict, enumTypeDict, getBackendEnum} = useBackendEnum();
  const actionRef = useRef<KfPagingTablePagePropsAction<Entity, EntityPageQueryParams>>();

  return (
    <KfPagingTablePage<Entity, EntityPageQueryParams, BizKfExtraProColumnType>
      actionRef={actionRef}
      entityTypeName={entityTypeName}
      pagingQuery={API.queryJournalSpecialColumns}
      opActionsDef={{
        single: {
          deleteOne: (record) => {
            KfModal.deleteConfirm(record.id, () => {
              API.deleteJournalSpecialColumn(record.id!).then((res) => {
                if (res.flag) {
                  message.info('删除成功');
                  actionRef.current?.reload();
                }
              });
            });
          },
          enableOne: (record) => {
            KfModal.confirm("启用", record.name as string, () => {
              API.enableJournalSpecialColumn(record.id!).then((res) => {
                if (res.flag) {
                  message.info('启用成功');
                  actionRef.current?.reload();
                }
              });
            });
          },
          disableOne: (record) => {
            KfModal.confirm("禁用", record.name as string, () => {
              API.disableJournalSpecialColumn(record.id!).then((res) => {
                if (res.flag) {
                  message.info('禁用成功');
                  actionRef.current?.reload();
                }
              });
            });
          },
        },
        multi: {
          deleteMultiple: (records) => {
            KfModal.batchActionConfirm('删除', records.length, () => {
              API.deleteJournalSpecialColumns(records.map((r) => r.id!)).then((res) => {
                if (res.flag) {
                  message.info('批量删除成功');
                  actionRef.current?.reload();
                }
              });
            });
          },
        },
      }}
      opFloatingsDef={{
        noargs: {
          createModal: {
            show: true,
            buttonText: '新增',
            fltRender: () => ({
              title: `新建`,
              content: <EntityQuickForm formType={'create'}/>,
            }),
          },
        },
        single: {
          modifyModal: (record) => ({
            title: `编辑`,
            content: <EntityQuickForm formType={'modify'} bizId={record.id}/>,
          }),
          viewModal: (record) => ({
            fltType: 'modal',
            title: `查看`,
            content: <EntityQuickForm formType={'view'} bizId={record.id}/>,
          }),
        },
      }}
      proTable={{
        tableBarActions: (action, rows) => {
          return [];
        },
        search: {
          optionRender: (searchConfig, formProps, dom) => [
            ...dom,
          ],
        },
      }}
      columns={[
        {
          title: '关键字',
          dataIndex: 'searchText',
          sorter: false,
          hideInTable: true,
          hideInForm: true,
          fieldProps: {
            placeholder: '名称和编码',
          },
        },
        {
          title: '序号',
          width: 50,
          key: '_seq_index_',
          align: 'center',
          hideInSearch: true,
          valueType: 'index',
          render: (dom, record, index) => {
            const pageInfo = actionRef.current?.queryParams();
            return ((pageInfo?.current || 1) - 1) * (pageInfo?.pageSize || 0) + (index + 1);
          },
        },
        {
          title: '名称',
          width: 300,
          ellipsis: true,
          dataIndex: nameof<Entity>((x) => x.name),
          sorter: false,
          hideInSearch: true,
        },
        {
          title: '简介',
          ellipsis: true,
          dataIndex: nameof<Entity>((x) => x.introduction),
          hideInSearch: true,
        },
        {
          title: '关联栏目',
          ellipsis: true,
          width: 80,
          dataIndex: nameof<Entity>((x) => x.journalColumnList),
          hideInSearch: true,
          render: (text, record) => {
            return record.journalColumnList?.length || 0;
          },
        },
        {
          title: '是否启用',
          width: 80,
          align: 'center',
          ellipsis: true,
          dataIndex: nameof<Entity>((x) => x.enable),
          hideInSearch: false,
          valueEnum: getBooleanToProTableValueEnum(),
        },
        {
          title: '操作',
          fixed: 'right',
          width: 130,
          dataIndex: 'option',
          valueType: 'option',
          render: (_, record) => (
            <Space size={0} split={<Divider type="vertical"/>}>
              {
                record.enable ? (
                  <a onClick={() => actionRef.current?.executeAction.single?.disableOne(record)}>
                    <Typography.Text type="danger">禁用</Typography.Text>
                  </a>
                ) : (
                  <a onClick={() => actionRef.current?.executeAction.single?.enableOne(record)}>
                    <Typography.Text type="success">启用</Typography.Text>
                  </a>
                )
              }
              <a onClick={() => actionRef.current?.openFloating.single?.modifyModal(record, 'modal')}>编辑</a>
              <a onClick={() => actionRef.current?.executeAction.single?.deleteOne(record)}>
                <Typography.Text type="danger">删除</Typography.Text>
              </a>
            </Space>
          ),
        },
      ]}
    />
  );
};

export default EntityTableList;
